<?php

namespace App\Console\Commands;

use App\Models\PayFee;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\Report;

class IncomeCountFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'income:count-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入日报表-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $start       = Carbon::yesterday()->timestamp;
        $end         = Carbon::today()->timestamp;
        $start       = mktime(0, 0, 0, 2, 16, 2019);
        $end         = mktime(0, 0, 0, 2, 17, 2019);
        $report_date = date('Y-m-d', $start);


        $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
            ->whereNotIn('park_code', config('custom.taxi_parks'))
            ->orderBy('sort_by', 'desc')
            ->pluck('park_code');

        //查询收入
        $res = PayFee::groupBy(['park_code', 'pay_type'])
            ->where('pay_time','>=',$start)
            ->where('pay_time','<',$end)
            ->select([
                \DB::raw('IFNULL(SUM(pay_amount),0) as pay_amount'),
                \DB::raw('sum(if(pay_amount>0,1,0)) as pay_number'),
                'park_code',
                'pay_type'
            ])
            ->whereIn('park_code',$parks)
            ->get()
            ->toArray();


        $taxi_res = PayFee::groupBy(['park_code', 'pay_type'])
            ->where('pay_time','>=',$start)
            ->where('pay_time','<',$end)
            ->select([
                \DB::raw('IFNULL(SUM(pay_amount),0) as pay_amount'),
                \DB::raw('sum(if(pay_amount>0,1,0)) as pay_number'),
                'park_code',
                'pay_type'
            ])
            ->whereIn('park_code',config('custom.taxi_parks'))
            ->where("pay_amount", "<>", 500)
            ->get()
            ->toArray();

        $newArray=[];
        $res = array_merge($res, $taxi_res);
        foreach ($res as $k=>$value) {
            $valueKey='charge_'.$value['pay_type'].'_'.$value['park_code'];
            $numberKey='number_'.$value['pay_type'].'_'.$value['park_code'];
            $newArray[$valueKey]=$value['pay_amount']/100;
            $newArray[$numberKey]=$value['pay_number'];
        }


        //存入数据库
        $report = Report::where('type', 'income_count_day_report')
                            ->where('report_date', $report_date)
                            ->first();
        if (!$report) {
            $report = new Report;
            $report->report_date = $report_date;
            $report->type = 'income_count_day_report';
        }

        $report->value = $newArray;
        $report->save();

        $this->info('收入日报表');

    }
}
